package com.tsingteng.cosfun.ui.mine;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseFragment;
import com.tsingteng.cosfun.ui.cosfun.PersonalFragment;
import com.tsingteng.cosfun.widget.CommonLinePageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * author :zhikuan
 * Date:18-8-15
 */
public class DraftsFragment extends BaseFragment {
    private ViewPager mViewPager;
    private CommonLinePageIndicator indicator;
    private List<Fragment> fragmentList;
    private MineFragmentPagerAdapter viewPagerAdapter;

    @Override
    protected void initView() {
        initViewPager();
    }

    private void initViewPager() {
        indicator = (CommonLinePageIndicator) rootView.findViewById(R.id.pager_indicator);
        mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
        indicator.setViewIds(new int[]{R.id.tab_line_layout,
                        R.id.tab_one, R.id.tab_two}
                , getResources().getColor(R.color.white),
                getResources().getColor(R.color.white));
        TextView textView1 = rootView.findViewById(R.id.tab_one);
        TextView textView2 = rootView.findViewById(R.id.tab_two);
        setSpanText(textView1, "6\n作品");
        setSpanText(textView2, "6\n段子");
        fragmentList = new ArrayList<Fragment>();

        fragmentList.add(new PersonalFragment());
        fragmentList.add(new PersonalFragment());
        viewPagerAdapter = new MineFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        mViewPager.setAdapter(viewPagerAdapter);

        indicator.setViewPager(mViewPager);
        indicator.setOnMyLineChangeListener(new CommonLinePageIndicator.OnCommonLineChangeListener() {
            @Override
            public void onCommonLineChange(int index) {
                switch (index) {
                    case 0:
                        break;
                    case 1:
                        break;

                }
            }

            @Override
            public void onCurrentClick(int index) {

            }

        });
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_drafts_mian_layout;
    }

    private void setSpanText(TextView textview, String text) {
        ///先构造SpannableString
        SpannableString spanString = new SpannableString(text);
        ForegroundColorSpan span = new ForegroundColorSpan(getResources().getColor(R.color.white_alpha_70));
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(0.8f);
        int length = text.length();
        spanString.setSpan(span, length - 2, length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spanString.setSpan(sizeSpan, length - 2, length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        textview.setText(spanString);
    }

    /**
     * pageAdapter
     */
    private class MineFragmentPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;

        public MineFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int i) {
            return fragmentList.get(i);
        }

        @Override
        public int getCount() {
            if (fragmentList != null) {
                return fragmentList.size();
            }
            return 0;
        }
    }
}
